package com.walnut.servlet;

import com.walnut.dao.FoodDao;
import com.walnut.dao.OrderDao;
import com.walnut.dao.impl.FoodDaoImpl;
import com.walnut.dao.impl.OrderDaoImpl;
import com.walnut.pojo.Food;
import com.walnut.pojo.Order;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/selectOrder")
public class SelectOrderServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = request.getServletContext();
        OrderDao orderDao = new OrderDaoImpl();

        // 获取查询参数
        String goodsName = request.getParameter("goodsName");
        String priceStr = request.getParameter("price");
        String goodsType = request.getParameter("goodsType");


        // 按照给定的条件查询食品
        List<Order> orders= new ArrayList<>();
        try {
            if (context.getAttribute("isAdmin").equals("0")){
                orders = orderDao.selectByConditions(goodsName, priceStr, goodsType, context.getAttribute("loginer").toString());
            }
            orders = orderDao.selectByConditions(goodsName, priceStr, goodsType, null);

            // 创建 HTML 表格内容
            StringBuilder tableContent = new StringBuilder();

            if (orders != null && !orders.isEmpty()) {
                int index = 1;
                for (Order order : orders) {
                    tableContent.append("<tr>")
                            .append("<td>").append(index).append("</td>")
                            .append("<td>").append(order.getGoodsName()).append("</td>")
                            .append("<td>").append(order.getPrice()).append("</td>")
                            .append("<td>").append(order.getGoodsType()).append("</td>")
                            .append("<td>").append(order.getPurchaser()).append("</td>")
                            .append("<td>").append(order.getCreateTime()).append("</td>")
                            .append("<td class='actions'>")
                            .append("<a href='javascript:void(0);' class='delete' onclick='confirmDelete(").append(order.getGoodsId()).append(")'>删除</a>")
                            .append("</td>")
                            .append("</tr>");
                    index++;
                }
            } else {
                tableContent.append("<tr><td colspan='6'>没有找到相关订单。</td></tr>");
            }
            // 输出 HTML 片段
            response.getWriter().write(tableContent.toString());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 暂时不需要处理 POST 请求
    }
}
